package com.eva.learn.concurrent.thinkInJava;

import java.util.concurrent.TimeUnit;

/**
 * 后台线程(守护线程)所创建线程默认为后台线程（可手动修改标识，但是风险很高，谨慎操作）
 * 
 * @author Evain.C
 *
 */

class Daemon implements Runnable {

	private Thread[] t = new Thread[10];

	@Override
	public void run() {
		for (int i = 0; i < t.length; i++) {
			t[i] = new Thread(new DaemonSpawn());
			t[i].start();
			System.out.print("DaemonSpawn " + i + " started, ");
		}
		for (int i = 0; i < t.length; i++) {
			System.out.print("t[" + i + "].isDaemon() =" + t[i].isDaemon()
					+ ", ");
		}
		while (true) {
			Thread.yield();
		}
	}
}

class DaemonSpawn implements Runnable {

	@Override
	public void run() {
		while (true) {
			Thread.yield();
		}
	}

}

public class Daemons {
	public static void main(String[] args) throws Exception {
		Thread d = new Thread(new Daemon());
		d.setDaemon(true);
		d.start();
		System.out.println("d.isDaemon() = " + d.isDaemon() + ", ");
		TimeUnit.MILLISECONDS.sleep(1);
	}
}
